﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Model;
using MsDAL;
using System.ComponentModel;
using Components;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public partial class Psn2a00Manager : Psn2a00Base
    {
        private static Psn2a00Manager _entity;

        public static Psn2a00Manager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new Psn2a00Manager();
                return _entity;
            }
        }

        #region 个人代码
        public string SaveData(Hashtable tb, string action, string usercode)
        {
            List<SqlApp> lst = new List<SqlApp>();

            Psn2a00 model = new Psn2a00();
            CommonUtil.GetInstance.CopyProperties(tb, model);
            model.Usercode = Context.GetAppByUserCode(usercode);

            if ("A" == action)
            {
                model.Listid = Sys0a60Manager.Entity.CreateSn("psn2a00_listid", usercode);
                lst.Add(this.Add(model));
            }
            else if ("D" == action)
            {
                lst.Add(this.Delete(model));

                //移除系统表中该用户工号
                lst.Add(Sys0a00Manager.Entity.RemoveEmpno(model.Listid));
            }

            return CommonManager.Entity.SaveData(lst);
        }

        public string SaveSetting(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            List<SqlApp> lst = new List<SqlApp>();

            Pot0b01 model = null;
            if ((dtl == null || dtl.Rows.Count == 0) && "A" == action) return "明细中没有数据，不需要保存！";

            string appstr = Context.GetAppByUserCode(usercode);

            Bas1b20DAL bas1b20dal = new Bas1b20DAL();
            Pot0b01DAL pot0b01dal = new Pot0b01DAL();

            //使用该类的原因是为了生成连续的单号
            using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
            {
                string ucode = Validation.ToString(tb["usercode"]);

                if ("A" == action || "D" == action)
                {
                    lst.Add(bas1b20dal.Delete(ucode)); //删除明细表

                    lst.Add(pot0b01dal.Delete(ucode)); //删除概要表
                }

                if ("A" == action)
                {

                    if (null != dtl)
                    {
                        foreach (DataRow dr in dtl.Rows)
                        {
                            model = new Pot0b01();
                            model.Usercode = ucode;
                            model.Frameid = Validation.ToString(dr["listid"]);

                            lst.Add(pot0b01dal.Add(model));
                        }
                    }

                    //回写促销销售商门店表
                    string shoplist = Validation.ToString(tb["shoplist"]);
                    string[] shops = shoplist.Split(',');
                    string[] item;
                    Bas1b20 pot = null;
                    foreach (string sellerAndShop in shops)
                    {
                        item = sellerAndShop.Split(';');
                        if (item.Length < 2) return "门店格式错误，请重新操作！";

                        pot = new Bas1b20();
                        pot.Usercode = ucode;
                        pot.Sellerid = item[0];
                        pot.Shopid = item[1];

                        lst.Add(bas1b20dal.Add(pot));
                    }
                }

                string msg = CommonManager.Entity.SaveData(lst);
                if (!string.IsNullOrEmpty(msg)) //是否提交事务
                    scope.Dispose();
                else
                    scope.Complete();

                return msg;
            }

            return null;
        }
        #endregion
    }
}



